<template>
    <el-form ref="formRef" style="max-width: 600px" :model="formData" :rules="rules" label-width="auto"
        class="demo-ruleForm" status-icon>
        <el-form-item label="类目的名称" prop="name">
            <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="类目图片" prop="img">
            <!-- <el-input v-model="formData.img" /> -->
            <Tuku v-model="formData.img"></Tuku>
        </el-form-item>
        <el-form-item label="类目排序" prop="seq">
            <!-- <el-input v-model="formData.seq" /> -->
            <el-input-number v-model="formData.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="父ID" prop="parentId">
            <!-- <el-input v-model="formData.parentId" /> -->
            <el-tree-select v-model="formData.parentId" :data="categoryDate" check-strictly :render-after-expand="false"
                style="width: 240px" />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">
                提交
            </el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import Tuku from '@/components/Tuku.vue';
import { categoryApi } from '@/api';
import { ElMessage } from 'element-plus'
import { useRouter,useRoute } from 'vue-router';
let router = useRouter()
let route=useRoute()
const formRef = ref()
const formData = reactive({
    id: 0,
    name: '',
    img: '',
    seq: 0,
    parentId: 0
})
// 表单验证
const rules = reactive({
    name: [
        { required: true, message: '分类的名称是必填项', trigger: 'blur' },
        { min: 2, max: 10, message: '名称的字母必须是 2 to 10', trigger: 'blur' },
    ],
})
const categoryDate = ref([
    {
        value: 0,
        label: '一级目录'
    }
])

//类目菜单级联
const CallcategoryTree = () => {
    categoryApi.select.call().then((res: any) => {
        // console.log("api的值=",res);
        res.forEach((item: any) => {
            item.value = item.id
            item.label = item.name
        });
        const categoryDate2 = res.reduce((hezi: any, currentvalue: any) => {
            //console.log(currentvalue);

            currentvalue.children = res.filter((item: any) => {
                return item.parentId === currentvalue.id
            })
            if (currentvalue.parentId === 0) {
                hezi.push(currentvalue)
            }
            return hezi;
        }, [])

        //console.log(categoryDate2);
        categoryDate.value = categoryDate2

    })
}

const CallcategoryData = () => { 
    categoryApi.select.call({ id: formData.id }).then((res: any) => { 
        console.log(res[0].name);
        formData.name = res[0].name
        formData.img = res[0].img
        formData.seq = res[0].seq
        formData.parentId= res[0].parentId
    })
}

onMounted(() => {
    console.log(route.query.id);
    formData.id= Number(route.query.id)
    CallcategoryTree()
    CallcategoryData()

})


//自己写的提交函数
const formsubmit = () => {
    categoryApi.update.call({
        id:formData.id,
        name: formData.name, img: formData.img,
        seq: formData.seq, parentId: formData.parentId
    }).then((res: any) => {
        console.log(res);
        ElMessage({
            showClose: true,
            message: '恭喜你修改成功',
        })
        router.push({ name: 'category-list' })
    })
}



const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            //点击提交按钮回调提交方法
            formsubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style scoped></style>